//
//  ClickMenu.swift
//  swiftProject
//
//  Created by ZhangSai on 2021/6/15.
//

import UIKit

protocol ClickMenuDelegate:class {
    func clickMenuAction(clickRow:Int)
}

class ClickMenu: UIScrollView {

    weak var menuDelegate:ClickMenuDelegate?

    var normalColor:UIColor!//默认颜色
    var selectColor:UIColor!//选中颜色
    var selectRow:Int!//当前选中的标签
    var menuFont:CGFloat!//默认字体
    var selectMenuFont:CGFloat!//选中字体

    var menuW:CGFloat!
    var countNum:Int!
    var itemCount:Int!

    
    init(frame: CGRect,itemNum:Int) {
        super.init(frame: frame)
        self.addSubview(self.selcetLine)
        itemCount = itemNum
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setup()  {
        normalColor = UIColor.init(hexString: "#666666")
        selectColor = UIColor.init(hexString: "#111111")
        menuFont = 14
    }
    
    func refreshDataSourceAction(dataSource:Array<String>) {
        let count = dataSource.count
        if itemCount > 0 {
            menuW = self.frame.width / CGFloat(itemCount)
            countNum = itemCount
        } else if count > 4 {
            let str = UserDefaults.standard.string(forKey:LanguageHeader.MySystemLanguage)
            if (str == LanguageHeader.ChinaLanguage) {
                countNum = 4
            } else {
                countNum = 3
            }
            menuW = self.frame.size.width / CGFloat(countNum)
        } else {
            menuW = self.frame.size.width / CGFloat(count)
        }
        var i:Int = 100
        for title in dataSource {
            let btn:UIButton = self.viewWithTag(i) as! UIButton
            i += 1
            btn.setTitle(title, for: .normal)
            btn.frame = CGRect.init(x: CGFloat((i - 101)) * menuW, y: 0, width: menuW, height: 39)
        }
        if count > 4 {
            self.contentSize = CGSize.init(width: CGFloat(count) * menuW, height: 0)
        } else {
            self.contentSize = CGSize.init(width: 0, height: 0)
        }
        self.selcetLine.frame = CGRect.init(x: 5, y: 39, width: menuW - 10, height: 1)
        
        let selectButton:UIButton = self.viewWithTag(selectRow) as! UIButton
        let lineX = selectButton.frame.minX + 5
                
        var frame = self.selcetLine.frame;
        frame.origin.x = lineX;
        self.selcetLine.frame = frame;
                
    }
    
    func setDataSourceAction(dataSource:Array<String>) {
        let count = dataSource.count
        if itemCount > 0 {
            menuW = self.frame.width / CGFloat(itemCount)
            countNum = itemCount
        } else if count > 4 {
            let str = UserDefaults.standard.string(forKey:LanguageHeader.MySystemLanguage)
            if (str == LanguageHeader.ChinaLanguage) {
                countNum = 4
            } else {
                countNum = 3
            }
            menuW = self.frame.size.width / CGFloat(countNum)
        } else {
            menuW = self.frame.size.width / CGFloat(count)
        }
        
        for i in 0..<count {
            let menuButton:UIButton = UIButton.init(type: .custom)
            menuButton.frame = CGRect.init(x: CGFloat(i) * menuW, y: 0, width: menuW, height: 39)
            menuButton.tag = 100 + i
            menuButton.addTarget(self, action: #selector(menuBtnClicked), for: .touchUpInside)
            menuButton.setTitle(dataSource[i], for: .normal)
            self.addSubview(menuButton)
            if i == 0 {
                selectRow = 100
                menuButton.setTitleColor(selectColor, for: .normal)
                menuButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: selectMenuFont)
            } else {
                menuButton.setTitleColor(normalColor, for: .normal)
                menuButton.titleLabel?.font = UIFont.systemFont(ofSize: menuFont)
            }
            if i == 0 {
                self.selcetLine.frame = CGRect.init(x: 5, y: 39, width: menuW - 10, height: 1)
            }
        }
        if count > 4 {
            self.contentSize = CGSize.init(width: CGFloat(count) * menuW, height: 0)
        } else {
            self.contentSize = CGSize.init(width: 0, height: 0)
        }
        self.selcetLine.frame = CGRect.init(x: 5, y: 39, width: menuW - 10, height: 1)
    }
    
    @objc func menuBtnClicked(menu:UIButton) {
        self.selectDidMenuAction(menuTag: menu.tag)
        self.menuDelegate?.clickMenuAction(clickRow: menu.tag - 100)
    }
    func selectDidMenuAction(menuTag:Int)  {
        if menuTag == selectRow {
            return
        }
        let selectButton:UIButton = self.viewWithTag(selectRow) as! UIButton
        let menuButton:UIButton = self.viewWithTag(menuTag) as! UIButton
        let lineX = menuButton.frame.minX + 5
        UIView.animate(withDuration: 0.2) {
            selectButton.setTitleColor(self.normalColor, for: .normal)
            selectButton.titleLabel?.font = UIFont.systemFont(ofSize: self.menuFont)
            
            menuButton.setTitleColor(self.selectColor, for: .normal)
            menuButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: self.selectMenuFont)
            
            var frame = self.selcetLine.frame;
            frame.origin.x = lineX;
            self.selcetLine.frame = frame
            self.slideSelectAction(menuTag: menuTag)
            self.selectRow = menuTag
        }
    }
    
    func slideSelectAction(menuTag:Int) {
        let selectButton:UIButton = self.viewWithTag(menuTag) as! UIButton
        //选中的实际位置
        let size_x = selectButton.frame.maxX
        //实际偏移量
        let content_x = self.contentOffset.x;
        
        //滑动分开 容易处理
        //往右滑
        if (menuTag > selectRow) {
            //1、实际滑动之后便宜 2、偏移量大于选中的应有的偏移量
            if (size_x - Constants.screenWidth > 0) {
                self.contentOffset = CGPoint.init(x: menuW * CGFloat((menuTag - (countNum == 4 ? 103:102))), y: 0)
            } else if (menuW * CGFloat((menuTag - 100)) < content_x) {
                self.contentOffset = CGPoint.init(x: menuW * CGFloat((menuTag - 100)), y: 0)
            }
        //往左滑
        } else {
            if(size_x <= content_x) {
                self.contentOffset = CGPoint.init(x: content_x - menuW, y: 0)
            } else if (menuW * CGFloat((menuTag - 103)) > content_x) {
                self.contentOffset = CGPoint.init(x: menuW * CGFloat((menuTag - (countNum == 4 ? 103:102))), y: 0)
            }
        }
    }
    
    lazy var selcetLine: UIView = {
        let selcetLine = UIView()
        selcetLine.backgroundColor = UIColor.init(hexString: "#FFCD33")
        return selcetLine
    }()
}
